package com.kjlink.internal.workreport.report.lookup;

import com.kjlink.internal.workreport.base.enums.UserType;
import com.kjlink.internal.workreport.helper.SessionHelper;
import com.kjlink.internal.workreport.report.model.WorkReport;
import com.kjlink.internal.workreport.user.model.User;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDate;

/**
 * 描述:
 *
 * @author Vic.xu
 * @since 2024-04-30 14:17
 */
public class WorkReportLookup extends WorkReport {

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate startDate;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate endDate;

    /**
     * 当前操作人id
     */
    private Integer operationId;

    /**
     * 当前操作人类型
     */
    private UserType userType;

    /**
     * 操作人小组id
     */
    private Integer groupId;

    /**
     * 是否项目经理
     */
    private Boolean devManager;


    /**
     * 设置权限
     */
    public void initAuthority() {
        User user = SessionHelper.currentUser();
        if (user != null) {

            // 处理项目日报
            if (devManager == null) {
                this.devManager = false;
            }
            // 当查看项目日报的时候 则不再设置权限，因为项目日报的权限已经处理过了：项目经理可以查看项目下的全部日报
            if (devManager) {
                markDevManager();
                return;
            }
            // 非项目日报的时候  设置权限
            this.userType = user.getType();

            // 处理权限
            switch (userType) {
                case normal:
                    // 组员只能看到自己的日报
                    this.operationId = user.getId();
                    break;
                case team:
                    //组长可以看到自己所在组的日报
                    Integer groupId = user.getGroupId();
                    if (groupId == null) {
                        groupId = -1;
                    }
                    this.groupId = groupId;
                    break;
                case admin:
                    //管理员 的groupId 是前端的查询字段
                    break;
            }


        }

    }

    /**
     * 标记为项目经理
     */
    public void markDevManager() {
        User user = SessionHelper.currentUser();
        this.devManager = true;
        if (user != null && Boolean.TRUE.equals(user.getDevManager())) {
            this.operationId = user.getId();
        } else {
            this.operationId = -1;
        }
    }

    public LocalDate getStartDate() {
        return startDate;
    }

    public void setStartDate(LocalDate startDate) {
        this.startDate = startDate;
    }

    public LocalDate getEndDate() {
        return endDate;
    }

    public void setEndDate(LocalDate endDate) {
        this.endDate = endDate;
    }

    public Integer getOperationId() {
        return operationId;
    }

    public void setOperationId(Integer operationId) {
        this.operationId = operationId;
    }

    public UserType getUserType() {
        return userType;
    }

    public void setUserType(UserType userType) {
        this.userType = userType;
    }

    public Integer getGroupId() {
        return groupId;
    }

    public void setGroupId(Integer groupId) {
        this.groupId = groupId;
    }

    public boolean isDevManager() {
        return Boolean.TRUE.equals(devManager);
    }

    public void setDevManager(Boolean devManager) {
        this.devManager = devManager;
    }

}


